import json
from flask import Flask
from flask_restful import Resource, Api
from flask import request
from Day08.models import db, User
from rediscluster import StrictRedisCluster
from redis.sentinel import Sentinel


app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:mysql@127.0.0.1/toutiao'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_ECHO'] = True
app.config['REDIS_CLUSTER'] = [
    {'host': '127.0.0.1', 'port': '7000'},
    {'host': '127.0.0.1', 'port': '7001'},
    {'host': '127.0.0.1', 'port': '7002'},

]
app.config['REDIS_SENTINELS'] = [
    ('127.0.0.1', '26380'),
    ('127.0.0.1', '26381'),
    ('127.0.0.1', '26382'),
]
app.config['REDIS_SENTINEL_SERVICE_NAME'] = 'mymaster'
app.redis_cluster = StrictRedisCluster(startup_nodes=app.config['REDIS_CLUSTER'])
_sentinel = Sentinel(app.config['REDIS_SENTINEL_SERVICE_NAME'])
db.init_app(app)

# 创建api对象,关联app
api = Api(app)
local_data = {}

# 定义视图类
class UserInfo(Resource):
    # def get(self):
    #     # 获取用户信息,直接查询数据库
    #     mobile = request.args.get('mobile')
    #     user = User.query.filter_by(mobile=mobile).first()
    #     data = {
    #         'user_id': user.id,
    #         'user_name': user.name,
    #         'mobile': user.mobile,
    #         'profile_photo': user.profile_photo
    #     }
    #     return data

    # def get(self):
    #     # 获取用户信息,基本架构
    #     mobile = request.args.get('mobile')
    #     # 缓存key
    #     redis_key = 'user:{}:profile'.format(mobile)
    #     # 从redis集群中获取数据
    #     ret_data = app.redis_cluster.get(redis_key)
    #     if ret_data:
    #         # 如果存在数据,直接返回
    #         return json.loads(ret_data.decode())
    #     user = User.query.filter_by(mobile=mobile).first()
    #     data = {
    #         'user_id': user.id,
    #         'user_name': user.name,
    #         'mobile': user.mobile,
    #         'profile_photo': user.profile_photo
    #     }
    #     # 设置到redis缓存中
    #     app.redis_cluster.set(redis_key, json.dumps(data))
    #     return data

    def get(self):
        # 获取用户信息,多级缓存架构
        mobile = request.args.get('mobile')
        # 缓存key
        redis_key = 'user:{}:profile'.format(mobile)

        global local_data
        # 先从本地缓存中获取数据,第一级缓存
        # ret_data = local_data.get(redis_key)
        # if ret_data:
        #     print('本地缓存')
        #     return ret_data
        #
        # # 从redis集群中获取数据,第二级缓存
        # ret_data = app.redis_cluster.get(redis_key)
        # if ret_data:
        #     # 如果存在数据,直接返回
        #     print('集群缓存')
        #     return json.loads(ret_data.decode())
        #
        # # 从redis主从中获取数据,第三级缓存
        # ret_data = app.redis_slave.get(redis_key)
        # if ret_data:
        #     # 如果存在数据,直接返回
        #     print('主从缓存')
        #     return json.loads(ret_data.decode())

        user = User.query.filter_by(mobile=mobile).first()
        data = {
            'user_id': user.id,
            'user_name': user.name,
            'mobile': user.mobile,
            'profile_photo': user.profile_photo
        }
        # 设置到本地全局变量中
        local_data[redis_key] = data
        # 设置到redis集群缓存中
        app.redis_cluster.set(redis_key, json.dumps(data))
        # 设置到redis主从缓存中
        app.redis_master.set(redis_key, json.dumps(data))
        return data

api.add_resource(UserInfo, '/userinfo')

if __name__ == '__main__':
    app.run(host='192.168.226.129', debug=True)


if __name__ == '__main__':
    app.run(debug=True)